package com.example.administrator.trains;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;


public class admin_change_user extends Activity implements View.OnClickListener{
    private Button change_user_button;
    private Button delete_user_button;

    private EditText edt_ID;
    private EditText edt_name;
    private EditText edt_code;
    private EditText edt_identity;
    private EditText edt_phone;

    private String old_ID;
    private String ID;
    private String old_name;
    private String name;
    private String old_code;
    private String code;
    private String old_identity;
    private String identity;
    private String old_phone;
    private String phone;
    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==2){
                Toast.makeText(admin_change_user.this,"修改失败",Toast.LENGTH_LONG).show();
            }
            if(msg.what==3){
                Toast.makeText(admin_change_user.this,"修改成功",Toast.LENGTH_LONG).show();
                admin_change_user.this.finish();
            }
            if(msg.what==4){
                Toast.makeText(admin_change_user.this,"服务器没有响应，请重新注册",Toast.LENGTH_LONG).show();
            }
            if(msg.what==5){
                Toast.makeText(admin_change_user.this,"服务器断开，尝试重连",Toast.LENGTH_LONG).show();
                Socket.connector.releaseSocket();
            }
            if(msg.what==6){
                Toast.makeText(admin_change_user.this,"删除成功",Toast.LENGTH_LONG).show();
                admin_change_user.this.finish();
            }
            if(msg.what==7){
                Toast.makeText(admin_change_user.this,"删除失败",Toast.LENGTH_LONG).show();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.change_user_msg);

        Intent i = getIntent();
        old_ID = i.getStringExtra("id");
        ID = i.getStringExtra("id");
        old_name = i.getStringExtra("name");
        name = i.getStringExtra("name");
        old_code = i.getStringExtra("code");
        code = i.getStringExtra("code");
        old_identity = i.getStringExtra("identity");
        identity = i.getStringExtra("identity");
        old_phone = i.getStringExtra("phone");
        phone = i.getStringExtra("phone");

        TextView text_id = (TextView)findViewById(R.id.ID);
        TextView text_name = (TextView)findViewById(R.id.name);
        TextView text_code = (TextView)findViewById(R.id.code);
        TextView text_identity = (TextView)findViewById(R.id.identify);
        TextView text_phone = (TextView)findViewById(R.id.phone);

        text_id.setText(ID.trim());
        text_name.setText(name.trim());
        text_code.setText(code.trim());
        text_identity.setText(identity.trim());
        text_phone.setText(phone.trim());

        change_user_button=(Button) findViewById(R.id.bt_change_user);
        delete_user_button=(Button) findViewById(R.id.bt_delete_user);
        change_user_button.setOnClickListener(this);
        delete_user_button.setOnClickListener(this);

        //按下改变颜色，以及执行命令
        change_user_button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape2));
                        break;
                    case MotionEvent.ACTION_UP:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
                        change_user_button.performClick();
                        break;
                }

                return true;
            }
        });
        //按下改变颜色，以及执行命令
        delete_user_button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape2));
                        break;
                    case MotionEvent.ACTION_UP:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
                        delete_user_button.performClick();
                        break;
                }

                return true;
            }
        });

        edt_ID = (EditText)findViewById(R.id.edt_get_id);
        edt_name = (EditText)findViewById(R.id.edt_get_name);
        edt_code = (EditText)findViewById(R.id.edt_input_passwd);
        edt_identity = (EditText)findViewById(R.id.edt_get_citizen_id);
        edt_phone = (EditText)findViewById(R.id.edt_get_phone_num);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //删除用户
            case R.id.bt_delete_user:
                try {
                    Socket.connector.send(Socket.connector.package_data(Socket.connector.LOGIN_CMD_TYPE,
                            Socket.connector.ADMIN_DELETE_USER, "00",
                            ID.trim() + CommunicationData.empty +
                            name.trim() + CommunicationData.empty +
                            code.trim() + CommunicationData.empty +
                            identity.trim() + CommunicationData.empty +
                            phone.trim() + CommunicationData.empty));
                    /*接收服务端是否成功接收信息的ack*/
                    Socket.connector.recv_ack();
                    if (CommunicationData.ack == "01") {
                        Socket.connector.recv_ack();
                        if (CommunicationData.ack == Socket.connector.ADMIN_DELETE_USER_SUCCESS_ACK) {
                            Message m = new Message();
                            m.what = 6;
                            myHandler.sendMessage(m);
                        }
                        if (CommunicationData.ack == Socket.connector.ADMIN_DELETE_USER_FALSE_ACK) {
                            Message m = new Message();
                            m.what = 7;
                            myHandler.sendMessage(m);
                        }
                    }
                } catch (IOException e){
                }
                break;
            case R.id.bt_change_user:
                if (Socket.mClient != null) {
                    try {
                        ID = edt_ID.getText().toString();
                        if (ID.length() == 0) {
                            ID = old_ID;
                        }
                        if (!ID.matches("[0-9a-zA-Z]{5,10}")) {
                            Toast.makeText(admin_change_user.this, "账号格式不对，请不要输入字母以外的字符,要求为5到10个字符组成", Toast.LENGTH_LONG).show();
                            return;
                        }

                        name = edt_name.getText().toString();
                        if (name.length() == 0) {
                            name = old_name;
                        }
                        if (!name.matches("[\\u4E00-\\u9FA5]{2,5}")) {
                            Toast.makeText(admin_change_user.this, "姓名格式不对，请输入中文,要求为二到五个中文", Toast.LENGTH_LONG).show();
                            return;
                        }

                        code = edt_code.getText().toString();
                        if (code.length() == 0) {
                            code = old_code;
                        }
                        if (!code.matches("[0-9a-zA-Z]{5,10}")) {
                            Toast.makeText(admin_change_user.this, "密码格式不对,请不要输入字母以外的字符,要求为5到10个字符组成", Toast.LENGTH_LONG).show();
                            return;
                        }

                        identity = edt_identity.getText().toString();
                        if (identity.length() == 0) {
                            identity = old_identity;
                        }
                        if (!identity.matches("[0-9]{18}")) {
                            Toast.makeText(admin_change_user.this, "身份证格式不对，请不要输入数字以外的字符,同时必须是18位数", Toast.LENGTH_LONG).show();
                            return;
                        }

                        phone = edt_phone.getText().toString();
                        if (phone.length() == 0) {
                            phone = old_phone;
                        }
                        if (!phone.matches("[0-9]{11}")) {
                            Toast.makeText(admin_change_user.this, "电话号码格式不对，请不要输入数字以外的字符,同时必须是11位数", Toast.LENGTH_LONG).show();
                            return;
                        }

                        /*发送请求*/
                        Socket.connector.send(Socket.connector.package_data(Socket.connector.LOGIN_CMD_TYPE,
                                Socket.connector.ADMIN_CHANGE_USER, "00",
                                old_ID.trim() + CommunicationData.empty +
                                ID.trim() + CommunicationData.empty +
                                code.trim() + CommunicationData.empty +
                                name.trim() + CommunicationData.empty +
                                identity.trim() + CommunicationData.empty +
                                phone.trim() + CommunicationData.empty));
                        /*接收服务端是否成功接收信息的ack*/
                        Socket.connector.recv_ack();
                        if (CommunicationData.ack == "01") {
                            /*接收服务端是否成功注册的ack*/
                            Socket.connector.recv_ack();
                            if (CommunicationData.ack == Socket.connector.ADMIN_CHANGE_USER_SUCCESS_ACK) {
                                Message m = new Message();
                                m.what = 3;
                                myHandler.sendMessage(m);
                            }
                            if (CommunicationData.ack == Socket.connector.ADMIN_CHANGE_USER_FALSE_ACK) {

                                Message m = new Message();
                                m.what = 2;
                                myHandler.sendMessage(m);
                            }

                            this.finish();
                        } else {
                            Message m = new Message();
                            m.what = 4;
                            myHandler.sendMessage(m);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Message m = new Message();
                    m.what = 5;
                    myHandler.sendMessage(m);
                }
                break;
            default:
                break;
        }
    }
}
